﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
using Entity;

namespace StudentAttendanceChecking
{
    public partial class frmViewReport : Form
    {
        public frmViewReport()
        {
            InitializeComponent();
        }
        DataSet dset = new DataSet();
        DataTable _tbl = new DataTable();
        dsReport.spViewStudentDataTable baocao;
        public static string su;
        public static int cl;
        public static int br;
        public void fillComboboxBranch()
        {
            using (SqlConnection conn = Connect.getConnection())
            {
                try
                {
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter("Select * from Branch", conn);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    cboBranch.DataSource = ds.Tables[0];
                    cboBranch.DisplayMember = "BranchName";
                    cboBranch.ValueMember = "BranchID";
                    conn.Close();

                }
                catch (Exception)
                {

                }

            }
        }
        private void frmViewReport_Load(object sender, EventArgs e)
        {
            fillComboboxBranch();
        }

        private void cboClass_SelectedValueChanged(object sender, EventArgs e)
        {
            SqlConnection conn = Connect.getConnection();
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select Subjects.SubjectName,Subjects.SubjectCode from Subjects inner join Attendants on Attendants.SubjectCode= Subjects.SubjectCode inner join Class on Attendants.ClassID= Class.ClassID	where Class.ClassName like " + "'" + cboClass.Text + "' group by Subjects.SubjectName,Subjects.SubjectCode", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cboSubject.DataSource = ds.Tables[0];
            cboSubject.DisplayMember = "SubjectName";
            cboSubject.ValueMember = "SubjectCode";
            conn.Close();
        }

        private void cboBranch_SelectedValueChanged(object sender, EventArgs e)
        {
            SqlConnection conn = Connect.getConnection();
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select Class.ClassName,Class.ClassID from Class inner join Branch on Class.BranchID= Branch.BranchID where Branch.BranchName like " + "'" + cboBranch.Text + "'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cboClass.DataSource = ds.Tables[0];
            cboClass.DisplayMember = "ClassName";
            cboClass.ValueMember = "ClassID";
            conn.Close();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {

                SqlConnection conn = Connect.getConnection();
                conn.Open();


                SqlCommand cmd = new SqlCommand("spViewStudent", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                Branch branch = new Branch();
                br=Convert.ToInt32(cboBranch.SelectedValue);
                cmd.Parameters.Add("@BranchID", SqlDbType.Int);
                cmd.Parameters["@BranchID"].Value =br ;

                cmd.Parameters.Add("@ClassID", SqlDbType.Int);
                Classs class1 = new Classs();
                cl = Convert.ToInt32(cboClass.SelectedValue);
                cmd.Parameters["@ClassID"].Value = cl;

                Subjects sub = new Subjects();
                su = cboSubject.SelectedValue.ToString();
                cmd.Parameters.Add("@SubjectCode", SqlDbType.VarChar);
                cmd.Parameters["@SubjectCode"].Value = su;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                //dset.Clear();
                //da.Fill(dset, "TH");
                _tbl.Clear();
                da.Fill(_tbl);

                grvReport.DataSource = _tbl;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                dset.Clear();
            }
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            try
            {
                Branch branch = new Branch();
                branch.BranchID = Convert.ToInt32(cboBranch.SelectedValue);
                Classs class1 = new Classs();
                class1.ClassID = Convert.ToInt32(cboClass.SelectedValue);
                Subjects sub = new Subjects();

                sub.SubjectCode = cboSubject.SelectedValue.ToString();
                rptReport document = new rptReport();
                baocao = new dsReportTableAdapters.spViewStudentTableAdapter().GetData(Convert.ToInt32(cboBranch.SelectedValue), Convert.ToInt32(cboClass.SelectedValue), sub.SubjectCode);
                document.SetDataSource((DataTable)baocao);
                for (int i = 0; i < baocao.Columns.Count-1; i++)
                {
                    document.DataDefinition.FormulaFields[i].Text = string.Format("{{spViewStudent.{0}}}", baocao.Columns[i].ColumnName);
                    ((CrystalDecisions.CrystalReports.Engine.TextObject)document.ReportDefinition.ReportObjects[string.Format("Text{0}", i + 1)]).Text = baocao.Columns[i].ColumnName;
                }
                crystalReportViewer1.ReportSource = document;
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.ToString());
                crystalReportViewer1.ReportSource = null;
            }

            //dsReport ds = new dsReport();
            //dsReportTableAdapters.spViewStudentTableAdapter adt1 = new dsReportTableAdapters.spViewStudentTableAdapter();
            //dsReport.spViewStudentDataTable tb1 = new dsReport.spViewStudentDataTable();
            //adt1.Fill(tb1,1, 3, "WFC#-I");

           
            //MessageBox.Show(tb1.Rows.Count.ToString());
            //ds.Tables.Add(tb1);

            //rptReport1.SetDataSource(ds.Tables[0]);

            //// rpt.SetDataSource(ds);
            //crystalReportViewer1.ReportSource = rptReport1;
            //crystalReportViewer1.Show();
           
        }
    }
}
